package game;

import engine.EngineBase;
import game.state.GameState;
import game.state.gameplay.Gameplay;
import game.state.menu.TitleMenu;

public class Game extends EngineBase 
{

    /* Members by way of extending EngineBase:
    protected boolean secSwitch;
    protected int fpsCounter;
    protected int secTick;
    protected int fpsTotal;
    protected int tick;
    protected float secCounter;
    protected float timeCycle;
    protected float timeTotal;
    protected float fpsInstant;
    protected Displays display;
    protected Prefs prefs;
    protected RenderEngine renderEngine;
    protected Inputs inputs;
    */
    
    private TitleMenu titleMenu;
    private Gameplay gameplay;
    private int currentState;
    
    public Game() 
    {
        super();
    }
    
    protected void onStart() 
    {
        titleMenu = new TitleMenu(display);
        gameplay = new Gameplay(display, renderEngine.glManager());
//        currentState = GameState.TITLE_MENU;
        currentState = GameState.GAMEPLAY;
    }
    
    protected void onUpdate() 
    {
        switch (currentState) 
        {
            case (GameState.TITLE_MENU): 
            {
                inputs.mouse().setCursorGrabbed(false);
                titleMenu.update(inputs.mouse(), renderEngine.dim2());
                if (titleMenu.isMovingOn()) 
                {
                    currentState = titleMenu.nextState();
                    titleMenu.reset();
                    if (currentState == GameState.GAMEPLAY) // If going into GAMEPLAY:
                    {
                        inputs.mouse().setPos(display.currentCX(), display.currentCY());
                    }
                }
                break;
            } 
            case (GameState.GAMEPLAY): 
            {
                inputs.mouse().setCursorGrabbed(true);
                gameplay.update(
                    inputs.mouse(), inputs.keys(), 
                    renderEngine.dim3(), renderEngine.dim2(), 
                    renderEngine.textBuffer(), 
                    renderEngine.glManager(), 
                    timeTotal, timeCycle, fpsTotal
                );
                if (gameplay.isMovingOn()) 
                {
                    currentState = gameplay.nextState();
                    gameplay.reset();
                }
                break;
            } 
            default: 
            {
                break;
            }
        }
    }

    protected void perSecond() 
    {
        gameplay.perSecond();
    }
    
    protected void onClose() 
    {
        
    }

}
